for (int i = 0; i < nums.size() - 2; i++) { // in case repeated results if (i > 0 && nums[i]==nums[i-1]) continue;
int target = -nums[i]; int left = i + 1; int right = nums.size() - 1; while (left < right) { auto sum = nums[left] + nums[right]; if (sum == target) { results.push_back({nums[i], nums[left], nums[right]}); left++; right--;
// in case repeated results while (nums[left] == nums[left-1] && left < right) { left++; } } elseif (sum < target) { left++; } else { right--; } } }